<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/11
 * Time: 下午12:11
 */
namespace app\http\middleware;

use app\common\model\AdminUserModel;
use app\common\model\AdminMenuModel;
use app\common\model\AuthGroupModel;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Session;

class AdminAuth
{
    public function handle($request, \Closure $next)
    {
        $arr=[
            'app'=>Request::module(),
            'controller'=>Request::controller(),
            'action'=>Request::action(),
        ];

        $sessionAdminId = Session::get('UID');
        if (!empty($sessionAdminId)) {
            $user = AdminUserModel::get($sessionAdminId);

            //检测方法是否加入权限
            if (AdminMenuModel::chackAction($arr)) {
                return redirect("/control/public/jump",merror("没有找到此项菜单，请添加！"));
            }

            if (!AuthGroupModel::check_auth($user['auth_id'], $arr) && $sessionAdminId != AdminUserModel::SUPER_USER_ID) {
                return redirect("/control/public/jump",merror("您没有访问权限！"));
            }
        }

        return $next($request);
    }
}